﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RussiaLibrary
{
    public class ZPolygon : Class_PolygonBase
    {
        public override bool Init(byte[,] Bytes_TetrisByte)//Z形方块初始化。
        {
//            int_State = Class_Common.GetRandomInt() % 4;
            int_State = 2;
            switch (int_State)
            {
                case 0:// z
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(0, 4), new KeyValuePair<int, int>(0, 5),
                                                      new KeyValuePair<int, int>(1, 5), new KeyValuePair<int, int>(1, 6));
                case 1://  |
                       // |
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(2, 4), new KeyValuePair<int, int>(1, 4),
                                                      new KeyValuePair<int, int>(1, 5), new KeyValuePair<int, int>(0, 5));
                case 2://   _
                       //  -
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(1, 4), new KeyValuePair<int, int>(1, 5),
                                                      new KeyValuePair<int, int>(0, 5), new KeyValuePair<int, int>(0, 6));
                case 3:// |
                       //  |
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(2, 5), new KeyValuePair<int, int>(1, 5),
                                                      new KeyValuePair<int, int>(1, 4), new KeyValuePair<int, int>(0, 4));
            }
            return false;
        }

        public override void Change(byte[,] Bytes_TetrisByte)
        {
            bool canChange = true;
            if (points[3].Key + 1 < Class_Common.Length && points[0].Key - 1 > 0
                && points[3].Value + 2 < Class_Common.Width && points[0].Value - 2 > 0
                ) //Y轴防止越界
            {
                for (int i = points[1].Key - 1; i < points[1].Key + 2; i++)//4x4的方格作为变形框，该方格里面有任何一处被占用，那么则不能变形
                {
                    for (int j = points[1].Value - 1; j <= points[1].Value + 2;j++ )
                    {
                        if (Bytes_TetrisByte[i, j] == 2)
                        {
                            canChange = false;
                        }
                    }
                }
               // canChange = false;
            }
            else {
                canChange = false;
            }

            if (true == canChange)
            {
                if (0 == int_State)
                {
                    List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                    for (int k = 0; k < points.Count; k++)
                    {
                        Bytes_TetrisByte[points[k].Key, points[k].Value] = 0;//首先清空原来的状态；
                        KeyValuePair<int, int> tempPoint;
                        switch (k)//1/2/3/4分别代表了4个点
                        {
                            case 0:
                                tempPoint = new KeyValuePair<int, int>(points[k].Key - 1, points[k].Value + 1);
                                newPoints.Add(tempPoint);
                                Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                break;
                            case 1:
                                newPoints.Add(points[1]);
                                Bytes_TetrisByte[points[1].Key, points[1].Value] = 1;
                                break;
                            case 2:
                                tempPoint = new KeyValuePair<int, int>(points[k].Key - 1, points[k].Value - 1);
                                newPoints.Add(tempPoint);
                                Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                break;
                            case 3:
                                tempPoint = new KeyValuePair<int, int>(points[k].Key, points[k].Value - 2);
                                newPoints.Add(tempPoint);
                                Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                break;
                            default:
                                break;
                        }
                    }
                    int_State = int_State + 1 % 2;
                    points = newPoints;
                }
                else if (1 == int_State)
                {
                    List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                    KeyValuePair<int, int> tempPoint;
                    for (int i = 0; i < 4; i++)
                    {
                        Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                        switch (i)
                        {
                            case 0:
                                tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value - 1);
                                newPoints.Add(tempPoint);
                                Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                break;
                            case 1:
                                newPoints.Add(points[1]);
                                Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                break;
                            case 2:
                                tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value + 1);
                                newPoints.Add(tempPoint);
                                Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                break;
                            case 3:
                                tempPoint = new KeyValuePair<int, int>(points[i].Key, points[i].Value + 2);
                                newPoints.Add(tempPoint);
                                Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                break;
                            default:
                                break;
                        }
                    }
                    int_State = int_State - 1;
                    points = newPoints;
                }
                else if (2 == int_State)
                {
                    List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                    KeyValuePair<int, int> tempPoint;
                    for (int i = 0; i < 4; i++)
                    {
                        Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                        switch (i)
                        {
                            case 0:
                                tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value + 1);
                                newPoints.Add(tempPoint);
                                Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                break;
                            case 1:
                                newPoints.Add(points[1]);
                                Bytes_TetrisByte[points[1].Key, points[1].Value] = 1;
                                break;
                            case 2:
                                tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value + 1);
                                newPoints.Add(tempPoint);
                                Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                break;
                            case 3:
                                tempPoint = new KeyValuePair<int, int>(points[i].Key + 2, points[i].Value);
                                newPoints.Add(tempPoint);
                                Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                break;
                            default:
                                break;

                        }
                    }
                    int_State = int_State + 1;
                    points = newPoints;
                }
                else if (3 == int_State)
                {
                    List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                    KeyValuePair<int, int> tempPoint;
                    for (int i = 0; i < 4; i++)
                    {
                        Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                        switch (i)
                        {
                            case 0:
                                tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value - 1);
                                newPoints.Add(tempPoint);
                                Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                break;
                            case 1:
                                newPoints.Add(points[1]);
                                Bytes_TetrisByte[points[1].Key, points[1].Value] = 1;
                                break;
                            case 2:
                                tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value - 1);
                                newPoints.Add(tempPoint);
                                Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                break;
                            case 3:
                                tempPoint = new KeyValuePair<int, int>(points[i].Key - 2, points[i].Value);
                                newPoints.Add(tempPoint);
                                Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                break;
                            default:
                                break;

                        }
                    }
                    int_State = int_State - 1;
                    points = newPoints;
                }
                else
                {
                    Console.Write("Z形方块状态错误");
                }

            }
            else {
                Console.Write("Z形方块不满足变形条件");
            }
            return;
        }
    }
}
